package cn.monkey.family.server.domain.uc.auth;

import cn.monkey.family.data.Result;
import cn.monkey.family.data.uc.auth.AuthRequest;
import cn.monkey.family.data.context.Context;
import cn.monkey.family.data.context.RequestContext;
import cn.monkey.family.data.uc.auth.wechat.WechatSignInRequest;
import cn.monkey.family.server.domain.uc.auth.wechat.IWechatService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("auth")
public class AuthController {
    final IAuthService authService;
    final IWechatService wechatService;

    public AuthController(AuthService authService, IWechatService wechatService) {
        this.authService = authService;
        this.wechatService = wechatService;
    }


    @PostMapping("signIn")
    Result<?> signIn(@Context(authorization = @Context.Option(required = false)) RequestContext requestContext,
                     @RequestBody AuthRequest authRequest) {
        return this.authService.authorize(requestContext, authRequest);
    }

    @PostMapping("/wechat/signIn")
    Result<?> wechatSignIn(@Context(authorization = @Context.Option(required = false)) RequestContext requestContext,
                           @RequestBody WechatSignInRequest signInRequest) {
        return this.wechatService.authorize(requestContext, signInRequest);
    }

    @DeleteMapping("signOut")
    Result<?> signOut(@Context RequestContext requestContext) {
        return this.authService.clearAuth(requestContext);
    }

    @PostMapping("temporary-token")
    Result<?> newTemporaryToken(@Context RequestContext requestContext) {
        return this.authService.newTemporaryToken(requestContext);
    }
}
